package com.stupidzhe.vbreview.service.impl;

import com.stupidzhe.vbreview.bean.PageBean;
import com.stupidzhe.vbreview.bean.ResultBean;
import com.stupidzhe.vbreview.bean.VBAssResultBean;
import com.stupidzhe.vbreview.dao.ClassMapper;
import com.stupidzhe.vbreview.dao.ProjectMapper;
import com.stupidzhe.vbreview.dao.UploadProjectMapper;
import com.stupidzhe.vbreview.domain.Project;
import com.stupidzhe.vbreview.domain.UploadProject;
import com.stupidzhe.vbreview.service.UploadProjectService;
import com.stupidzhe.vbreview.util.TimeUtil;
import com.stupidzhe.vbreview.util.VBAssUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UploadProjectServiceImpl implements UploadProjectService {

    private final UploadProjectMapper uploadProjectMapper;

    private final ClassMapper classMapper;

    private final ProjectMapper projectMapper;

    public UploadProjectServiceImpl(UploadProjectMapper uploadProjectMapper, ClassMapper classMapper, ProjectMapper projectMapper) {
        this.uploadProjectMapper = uploadProjectMapper;
        this.classMapper = classMapper;
        this.projectMapper = projectMapper;
    }

    @Override
    public ResultBean addUploadProject(Integer studentId, Integer projectId, String projectLocation) {

        Project project = projectMapper.selectByPrimaryKey(projectId);

        if (isMax(projectId, studentId)) {
            return new ResultBean(false, "作业上传已达到最大限度");
        }

        UploadProject uploadProject = new UploadProject();
        uploadProject.setLocation(projectLocation);
        uploadProject.setStudentId(studentId);
        uploadProject.setProjectId(projectId);
        uploadProject.setUpTime(TimeUtil.getCurrentTime());

        // 测评
        VBAssResultBean vbAssResultBean = VBAssUtil.analyzeVB(projectLocation, project.getDemoLocation(), project.getClassification());

        if (project.getClassification().equals("0")) {
            if (!vbAssResultBean.isStatus()) {
                uploadProject.setResult("未通过");
            } else {
                uploadProject.setResult("通过");
            }
        } else {
            uploadProject.setResult(vbAssResultBean.getScore());
        }
        uploadProject.setJudgement(vbAssResultBean.getResult());

        try {
            int res = uploadProjectMapper.insert(uploadProject);
            if (res == 0) {
                return new ResultBean(false, "作业上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBean(false, "作业上传失败");
        }
        return new ResultBean(true, vbAssResultBean.getResult());
    }

    @Override
    public boolean isMax(Integer projectId, Integer userId) {
        Project project = projectMapper.selectByPrimaryKey(projectId);

        List<UploadProject> uploadProjectList = uploadProjectMapper.selectByStudentIdandProjectId(userId, projectId);

        return uploadProjectList.size() >= project.getUpTimesMax();
    }

    @Override
    public boolean isPass(Integer projectId, Integer userId) {
        List<UploadProject> uploadProjectList = uploadProjectMapper.selectByStudentIdandProjectId(userId, projectId);
        boolean res = false;
        for (UploadProject project : uploadProjectList) {
            if (project.getResult().equals("通过") || project.getResult().equals("30.0")) {
                res = true;
            }
        }
        return res;
    }

    @Override
    public List<UploadProject> getList(Integer projectId, Integer userId) {
        return uploadProjectMapper.selectByStudentIdandProjectId(userId, projectId);

    }

    @Override
    public PageBean<UploadProject> getUploadProjectByPage(Integer pageNum, Integer teacherId, String keyword, Integer studentId) {
        if (pageNum < 1) pageNum = 1;
        PageBean<UploadProject> pageBean = new PageBean<>();
        pageBean.setPageNum(pageNum);
        List<UploadProject> res = uploadProjectMapper.getPageContent(pageBean.getStart(), pageBean.getSize(), keyword, teacherId, studentId);
        pageBean.setRowCount(uploadProjectMapper.getCount(keyword, teacherId, studentId));
        pageBean.setContent(res);
        return pageBean;
    }

    @Override
    public ResultBean changeResult(Integer userId, Integer uploadId, String result) {
        UploadProject uploadProject = uploadProjectMapper.selectByPrimaryKey(uploadId);
        if (!"未通过".equals(result) && !"通过".equals(result)) {
            return new ResultBean(false, "状态错误");
        }
        uploadProject.setResult(result);
        uploadProject.setJudgement(uploadProject.getJudgement() + "(教师手动通过)");
        int res = uploadProjectMapper.update(uploadProject);
        if (res != 0)
            return new ResultBean(true, "修改成功");
        return new ResultBean(false, "修改失败");
    }
}
